<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\RefereeBlack;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Actions\Grid\BlackDownloadTemplate;
use App\Admin\Forms\BlackImportForm;

class RefereeBlackController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new RefereeBlack(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('phone');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(2);
                $filter->like('phone')->width(3);
            });
            $grid->disableViewButton();
            // 在工具栏添加操作按钮，用于上传Excel文件及下载导入模板
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(Modal::make()
                    // 弹窗标题
                    ->title('上传文件')
                    // 按钮
                    ->button('<button class="btn btn-primary"><i class="feather icon-upload"></i>&nbsp;&nbsp;导入数据</button>')
                    // 弹窗内容
                    ->body(BlackImportForm::make()));
                // 下载导入模板
                $tools->append(BlackDownloadTemplate::make());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new RefereeBlack(), function (Show $show) {
            $show->field('id');
            $show->field('phone');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Form::make(new RefereeBlack(), function (Form $form) {
            $form->display('id');

            $table = 'referee_blacks';
            $id = $form->getKey();
            $form->text('phone', '手机')
                ->required()
                ->creationRules(['required', 'phone:CN,mobile', "unique:{$table}"])
                ->updateRules(['required', 'phone:CN,mobile', "unique:{$table},phone,{$id}"]);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
